const { ipcRenderer } = require('electron');

class EventManager {
    constructor(app) {
        this.app = app;
    }

    setupEventListeners() {
        this.setupNavigationEvents();
        this.setupViewToggleEvents();
        this.setupTaskInputEvents();
        this.setupWindowControlEvents();
    }

    setupNavigationEvents() {
        // 导航切换
        document.querySelectorAll('.nav-item').forEach(item => {
            item.addEventListener('click', (e) => {
                const category = e.currentTarget.dataset.category;
                if (category) {
                    this.app.switchCategory(category);
                }
            });
        });
    }

    setupViewToggleEvents() {
        // 视图切换
        document.querySelectorAll('.toggle-btn').forEach(btn => {
            btn.addEventListener('click', (e) => {
                const isKanban = e.target.textContent.trim() === '看板';
                this.app.switchView(isKanban ? 'kanban' : 'list');
            });
        });
    }

    setupTaskInputEvents() {
        // 任务输入
        const taskInput = document.getElementById('taskInput');
        if (taskInput) {
            taskInput.addEventListener('keypress', (e) => {
                if (e.key === 'Enter' && e.target.value.trim()) {
                    this.app.addTask(
                        e.target.value.trim(),
                        this.app.inputController.selectedPomodoros,
                        this.app.inputController.selectedDueDate
                    );
                    e.target.value = '';
                    this.app.inputController.resetSelections();
                }
            });
        }

        // 番茄选择器
        this.app.inputController.setupPomodoroSelector();

        // 到期时间选择器
        this.app.inputController.setupDueDateSelector();
    }

    setupWindowControlEvents() {
        // 窗口控制
        document.querySelector('.minimize-btn').addEventListener('click', () => {
            ipcRenderer.send('window-minimize');
        });

        document.querySelector('.maximize-btn').addEventListener('click', () => {
            ipcRenderer.send('window-maximize');
        });

        document.querySelector('.close-btn').addEventListener('click', () => {
            ipcRenderer.send('window-close');
        });
    }
}

module.exports = EventManager;